package org.ala.config;

import java.util.Arrays;

import javax.sql.DataSource;

import org.ala.tiktools.mybatis.expand.ConfigurationExpander;
import org.ala.tiktools.mybatis.expand.MapperRegistryExpander;
import org.ala.tiktools.sharddb.mybatis.interceptor.ShardInterceptor;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * @author ala
 * @date 2025-01-06 14:11
 */
@Configuration
public class TestConfiguration {


    /**
     *
     */
    @Bean
    public ConfigurationExpander configurationExpander() {
        ConfigurationExpander ce = new ConfigurationExpander();

        //  mapper注册机
        MapperRegistryExpander mapperRegistryExpander = new MapperRegistryExpander(ce);
        //  追加注解拦截器（顺序就是追加时的顺序）
        mapperRegistryExpander.setAnnotationInterceptorChain(Arrays.asList(
                //  分库分表拦截器
                new ShardInterceptor()
        ));
        ce.setMapperRegistry(mapperRegistryExpander);

        return ce;
    }

    /**
     * SqlSessionFactory
     * @throws Exception
     */
    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(ConfigurationExpander configurationExpander, DataSource dataSource) throws Exception {
        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
        fb.setConfiguration(configurationExpander);
        fb.setDataSource(dataSource);
        fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:org/ala/**/*.xml"));

        SqlSessionFactory sf = fb.getObject();
        //  没必要，用的时候再建来得及
//        SqlSessionTemplateHolder.setInstance(new SqlSessionTemplate(sf));
        return sf;
    }

    /**
     * mapper接口扫描路径
     */
    @Bean("mapperScannerConfigurer")
    public MapperScannerConfigurer mapperScannerConfigurer(SqlSessionFactory sqlSessionFactory){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("org.ala");
        msc.setAnnotationClass(Mapper.class);
        msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
        msc.setSqlSessionTemplateBeanName("sqlSessionTemplate");
//        msc.setNameGenerator(new BeanNameGenerator() {
//            public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
//                String[] beanClassNames = definition.getBeanClassName().split("\\.");
//                String beanName = "testDB1" + beanClassNames[beanClassNames.length - 1];
//                return beanName;
//            }
//        });
        return msc;
    }

    /**
     * sqlSessionTemplate
     */
    @Bean("sqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        SqlSessionTemplate tpl = new SqlSessionTemplate(sqlSessionFactory);
        return tpl;
    }
}
